#!/bin/bash

# 假期数组
holidays=( 
    # 2022年法定假日和周末
       "2022-01-01" "2022-01-02" "2022-01-03" "2022-01-08" "2022-01-09" "2022-01-15" "2022-01-16"
       "2022-01-22" "2022-01-23" "2022-01-31"
       "2022-02-01" "2022-02-02" "2022-02-03" "2022-02-04" "2022-02-05" "2022-02-06" "2022-02-12"
       "2022-02-13" "2022-02-19" "2022-02-20" "2022-02-26" "2022-02-27"
       "2022-03-05" "2022-03-06" "2022-03-12" "2022-03-13" "2022-03-19" "2022-03-20" "2022-03-26"
       "2022-03-27"
       "2022-04-03" "2022-04-04" "2022-04-05" "2022-04-09" "2022-04-10" "2022-04-16" "2022-04-17"
       "2022-04-23" "2022-04-30"
       "2022-05-01" "2022-05-02" "2022-05-03" "2022-05-08" "2022-05-14" "2022-05-15" "2022-05-21"
       "2022-05-22" "2022-05-28" "2022-05-29"
       "2022-06-03" "2022-06-04" "2022-06-05" "2022-06-11" "2022-06-12" "2022-06-18" "2022-06-19"
       "2022-06-25" "2022-06-26"
       "2022-07-02" "2022-07-03" "2022-07-09" "2022-07-10" "2022-07-16" "2022-07-17" "2022-07-23"
       "2022-07-24" "2022-07-30" "2022-07-31"
       "2022-08-06" "2022-08-07" "2022-08-13" "2022-08-14" "2022-08-20" "2022-08-21" "2022-08-27"
       "2022-08-28"
       "2022-09-03" "2022-09-04" "2022-09-10" "2022-09-11" "2022-09-12" "2022-09-17" "2022-09-18"
       "2022-09-24" "2022-09-25"
       "2022-10-01" "2022-10-02" "2022-10-03" "2022-10-04" "2022-10-05" "2022-10-06" "2022-10-07"
       "2022-10-15" "2022-10-16" "2022-10-22" "2022-10-23" "2022-10-29" "2022-10-30"
       "2022-11-05" "2022-11-06" "2022-11-12" "2022-11-13" "2022-11-19" "2022-11-20" "2022-11-26"
       "2022-11-27"
       "2022-12-03" "2022-12-04" "2022-12-10" "2022-12-11" "2022-12-17" "2022-12-18" "2022-12-24"
       "2022-12-25" "2022-12-31"

       # 2023年法定假日和周末
       "2023-01-01" "2023-01-02" "2023-01-07" "2023-01-08" "2023-01-14" "2023-01-15" "2023-01-21"
       "2023-01-22" "2023-01-23" "2023-01-24" "2023-01-25" "2023-01-26" "2023-01-27"
       "2023-02-04" "2023-02-05" "2023-02-11" "2023-02-12" "2023-02-18" "2023-02-19" "2023-02-25"
       "2023-02-26"
       "2023-03-04" "2023-03-05" "2023-03-11" "2023-03-12" "2023-03-18" "2023-03-19" "2023-03-25"
       "2023-03-26"
       "2023-04-01" "2023-04-02" "2023-04-05" "2023-04-08" "2023-04-09" "2023-04-15" "2023-04-16"
       "2023-04-22" "2023-04-29" "2023-04-30"
       "2023-05-01" "2023-05-02" "2023-05-03" "2023-05-07" "2023-05-13" "2023-05-14" "2023-05-20"
       "2023-05-21" "2023-05-27" "2023-05-28"
       "2023-06-03" "2023-06-04" "2023-06-10" "2023-06-11" "2023-06-17" "2023-06-18" "2023-06-22"
       "2023-06-23" "2023-06-24"
       "2023-07-01" "2023-07-02" "2023-07-08" "2023-07-09" "2023-07-15" "2023-07-16" "2023-07-22"
       "2023-07-23" "2023-07-29" "2023-07-30"
       "2023-08-05" "2023-08-06" "2023-08-12" "2023-08-13" "2023-08-19" "2023-08-20" "2023-08-26"
       "2023-08-27"
       "2023-09-02" "2023-09-03" "2023-09-09" "2023-09-10" "2023-09-16" "2023-09-17" "2023-09-23"
       "2023-09-24" "2023-09-29" "2023-09-30"
       "2023-10-01" "2023-10-02" "2023-10-03" "2023-10-04" "2023-10-05" "2023-10-06"
       "2023-10-14" "2023-10-15" "2023-10-21" "2023-10-22" "2023-10-28" "2023-10-29"
       "2023-11-04" "2023-11-05" "2023-11-11" "2023-11-12" "2023-11-18" "2023-11-19" "2023-11-25"
       "2023-11-26"
       "2023-12-02" "2023-12-03" "2023-12-09" "2023-12-10" "2023-12-16" "2023-12-17" "2023-12-23"
       "2023-12-24" "2023-12-30" "2023-12-31"

       # 2024年法定假日和周末
       "2024-01-01" "2024-01-06" "2024-01-07" "2024-01-13" "2024-01-14" "2024-01-20" "2024-01-21"
       "2024-01-27" "2024-01-28"
       "2024-02-03" "2024-02-10" "2024-02-11" "2024-02-12" "2024-02-13" "2024-02-14" "2024-02-15"
       "2024-02-16" "2024-02-17" "2024-02-24" "2024-02-25"
       "2024-03-02" "2024-03-03" "2024-03-09" "2024-03-10" "2024-03-16" "2024-03-17" "2024-03-23"
       "2024-03-24" "2024-03-30" "2024-03-31"
       "2024-04-04" "2024-04-05" "2024-04-06" "2024-04-13" "2024-04-14" "2024-04-20" "2024-04-21"
       "2024-04-27"
       "2024-05-01" "2024-05-02" "2024-05-03" "2024-05-04" "2024-05-05" "2024-05-12" "2024-05-18"
       "2024-05-19" "2024-05-25" "2024-05-26"
       "2024-06-01" "2024-06-02" "2024-06-08" "2024-06-09" "2024-06-10" "2024-06-15" "2024-06-16"
       "2024-06-22" "2024-06-23" "2024-06-29" "2024-06-30"
       "2024-07-06" "2024-07-07" "2024-07-13" "2024-07-14" "2024-07-20" "2024-07-21" "2024-07-27"
       "2024-07-28"
       "2024-08-03" "2024-08-04" "2024-08-10" "2024-08-11" "2024-08-17" "2024-08-18" "2024-08-24"
       "2024-08-25" "2024-08-31"
       "2024-09-01" "2024-09-07" "2024-09-08" "2024-09-15" "2024-09-16" "2024-09-17" "2024-09-21"
       "2024-09-22" "2024-09-28"
       "2024-10-01" "2024-10-02" "2024-10-03" "2024-10-04" "2024-10-05" "2024-10-06" "2024-10-13"
       "2024-10-19" "2024-10-20" "2024-10-26" "2024-10-27"
       "2024-11-02" "2024-11-03" "2024-11-09" "2024-11-10" "2024-11-16" "2024-11-17" "2024-11-23"
       "2024-11-24" "2024-11-30"
       "2024-12-01" "2024-12-07" "2024-12-08" "2024-12-14" "2024-12-15" "2024-12-21" "2024-12-22"
       "2024-12-28" "2024-12-29"
)
# 文件路径
filePath="./commit_hashes_and_times.txt"

# 获取开始和结束日期
start_date="2024-01-01"
end_date="2024-09-30"

# 将 start_date 和 end_date 转换为时间戳
start_timestamp=$(date -d "$start_date" +%s)
end_timestamp=$(date -d "$end_date" +%s)

current_date="$start_date"
current_timestamp=$(date -d "$current_date" +%s)

# 计算工作日天数
workdays=0
while [[ "$current_timestamp" -le "$end_timestamp" ]]; do
    # 判断是否是工作日且不在假期内
    if [[ ! " ${holidays[@]} " =~ " $current_date " ]]; then
        workdays=$((workdays + 1))
    fi
    current_timestamp=$((current_timestamp + 86400))  # 递增一天
    current_date=$(date -d @"$current_timestamp" "+%Y-%m-%d")
done

# 计算 x 和 y
total=$(wc -l < "$filePath")
x=$((total / workdays))
y=$((total % workdays))

# 存储所有生成的时间
times=()

# 生成新的时间
current_date="$start_date"
current_timestamp=$(date -d "$current_date" +%s)

while [[ "$current_timestamp" -le "$end_timestamp" ]]; do
    # 判断是否是工作日且不在假期内
    if [[ ! " ${holidays[@]} " =~ " $current_date " ]]; then
        num_times=$x
        if ((y > 0)); then
            num_times=$((x + 1))
            y=$((y - 1))
        fi

       # 生成 num_times 个时间
        for ((i=0; i<num_times; i++)); do
            # 随机生成一个时间在9:00到18:00之间
            hour=$((9 + RANDOM % 9))
            minute=$((RANDOM % 60))
            second=$((RANDOM % 60))
            new_time=$(date -d "$current_date $hour:$minute:$second" "+%Y-%m-%d %H:%M:%S")

            # 保存生成的时间
            times+=("$new_time")
        done
    fi
    current_timestamp=$((current_timestamp + 86400))  # 递增一天
    current_date=$(date -d @"$current_timestamp" "+%Y-%m-%d")
done

# 按时间排序
IFS=$'\n' sorted_times=($(sort <<<"${times[*]}"))
unset IFS

# 替换原文件中的时间，保持顺序
commit_index=1
for new_time in "${sorted_times[@]}"; do
    # 获取当前行的哈希值
    commit_hash=$(sed -n "${commit_index}p" "$filePath" | cut -d, -f1)

    # 获取当前行的时间戳
    commit_time=$(sed -n "${commit_index}p" "$filePath" | cut -d, -f2)

    # 检查是否成功提取 commit_hash
    if [[ -z "$commit_hash" ]]; then
        echo "Error: Failed to extract commit hash for line $commit_index"
        exit 1
    fi

    # 使用 awk 替换当前行的时间
    awk -v line_index="$commit_index" -v commit_hash="$commit_hash" -v commit_time="$commit_time" -v new_time="$new_time" '
        NR == line_index {print commit_hash "," commit_time "," new_time}  # 替换时间，保留哈希值
        NR != line_index {print $0}  # 其他行保持不变
    ' "$filePath" > temp_file && mv temp_file "$filePath"

    commit_index=$((commit_index + 1))  # 行号递增
done
# 根据存在的时间，生成新的时间(排除工作日和法定节假日)
# 根据现成的txt文件，生成指定返回内的随机时间
# sh generateTime.sh a.txt
